{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2.6584e+23, 5.1126e+22, 3.2919e-09],\n",
      "        [8.1718e+20, 4.1883e-11, 4.2330e+21]])\n",
      "tensor([[[4.5095e+27, 7.6831e+31, 4.7429e+30],\n",
      "         [1.7053e+28, 1.6020e-19, 4.4721e+21]],\n",
      "\n",
      "        [[6.2625e+22, 4.7428e+30, 4.0092e-08],\n",
      "         [2.9508e+29, 7.5556e+31, 1.9081e+17]],\n",
      "\n",
      "        [[6.0047e+31, 4.2964e+24, 1.7743e+28],\n",
      "         [1.3458e-14, 2.5320e-12, 1.7034e+28]],\n",
      "\n",
      "        [[1.8062e+28, 5.5324e-14, 2.3083e-12],\n",
      "         [1.8590e+34, 7.7767e+31, 1.7181e+19]]])\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "\n",
    "x = torch.Tensor(2, 3)\n",
    "print(x)\n",
    "y = torch.Tensor(4, 2, 3)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Tensor的加法(四种)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.6195, 1.1260, 1.3657],\n",
      "        [0.9469, 1.6460, 1.1853],\n",
      "        [1.1176, 0.9027, 1.3680],\n",
      "        [0.7402, 1.4037, 0.5642],\n",
      "        [0.7427, 0.9327, 1.6934]])\n",
      "tensor([[0.6195, 1.1260, 1.3657],\n",
      "        [0.9469, 1.6460, 1.1853],\n",
      "        [1.1176, 0.9027, 1.3680],\n",
      "        [0.7402, 1.4037, 0.5642],\n",
      "        [0.7427, 0.9327, 1.6934]])\n",
      "tensor([[0.6195, 1.1260, 1.3657],\n",
      "        [0.9469, 1.6460, 1.1853],\n",
      "        [1.1176, 0.9027, 1.3680],\n",
      "        [0.7402, 1.4037, 0.5642],\n",
      "        [0.7427, 0.9327, 1.6934]])\n",
      "tensor([[0.6195, 1.1260, 1.3657],\n",
      "        [0.9469, 1.6460, 1.1853],\n",
      "        [1.1176, 0.9027, 1.3680],\n",
      "        [0.7402, 1.4037, 0.5642],\n",
      "        [0.7427, 0.9327, 1.6934]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand(5, 3)\n",
    "b = torch.rand(5, 3)\n",
    "print(a + b)\n",
    "print(torch.add(a, b))\n",
    "result = torch.Tensor(5, 3)\n",
    "print(torch.add(a, b, out=result))\n",
    "print(b.add_(a))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 2., 3.],\n",
      "        [4., 5., 6.]])\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(torch.Tensor(a))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]])\n",
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([[0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]])\n",
      "tensor([[0.3326, 0.3366, 0.7317, 0.4818],\n",
      "        [0.7273, 0.1529, 0.2450, 0.1754],\n",
      "        [0.7597, 0.6488, 0.0478, 0.4342]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "print(torch.empty(3, 4))\n",
    "print(torch.ones([3, 4]))\n",
    "print(torch.zeros([3, 4]))\n",
    "print(torch.rand([3, 4]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[2, 0, 0, 2],\n        [2, 1, 2, 0],\n        [0, 1, 2, 0]])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randint(low=0, high=3, size=[3, 4])\n",
    "# low是最低值,3是最大值,size是范围大小"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([3., 4.])\n",
      "3.0\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([3.], dtype=float32)"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = torch.Tensor([3, 4])\n",
    "print(t1)\n",
    "# print(t1.item())\n",
    "t2 = torch.Tensor([3])\n",
    "print(t2.item())\n",
    "t2.numpy()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3])\n",
      "torch.Size([3, 2])\n",
      "tensor([[  1.,   2.,   3.],\n",
      "        [  4., 100.,   6.]])\n",
      "tensor([[  1.,   2.],\n",
      "        [  3.,   4.],\n",
      "        [100.,   6.]])\n"
     ]
    }
   ],
   "source": [
    "t1 = torch.Tensor([[1, 2, 3], [4, 5, 6]])\n",
    "print(t1.size())\n",
    "t2 = t1.view((3, 2))\n",
    "print(t2.size())\n",
    "t1[1][1] = 100\n",
    "print(t1)\n",
    "# 浅拷贝,t1改变,t2也随之改变\n",
    "print(t2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 4])\n",
      "tensor([[[ 0.,  1.,  2.,  3.],\n",
      "         [ 4.,  5.,  6.,  7.],\n",
      "         [ 8.,  9., 10., 11.]],\n",
      "\n",
      "        [[12., 13., 14., 15.],\n",
      "         [16., 17., 18., 19.],\n",
      "         [20., 21., 22., 23.]]])\n",
      "torch.Size([3, 2, 4])\n",
      "tensor([[[ 0.,  1.,  2.,  3.],\n",
      "         [12., 13., 14., 15.]],\n",
      "\n",
      "        [[ 4.,  5.,  6.,  7.],\n",
      "         [16., 17., 18., 19.]],\n",
      "\n",
      "        [[ 8.,  9., 10., 11.],\n",
      "         [20., 21., 22., 23.]]])\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "t3=torch.Tensor(np.arange(24).reshape((2,3,4)))\n",
    "print(t3.size())\n",
    "print(t3)\n",
    "t4=t3.transpose(0,1)\n",
    "print(t4.size())\n",
    "print(t4)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 2, 3])\n",
      "torch.Size([3, 1, 2])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy    as np\n",
    "\n",
    "a=np.array([[[1,2,3],[4,5,6]]])\n",
    "\n",
    "unpermuted=torch.tensor(a)\n",
    "print(unpermuted.size())  #  ——>  torch.Size([1, 2, 3])\n",
    "\n",
    "permuted=unpermuted.permute(2,0,1)\n",
    "print(permuted.size())     #  ——>  torch.Size([3, 1, 2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n",
      "torch.float32\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy    as np\n",
    "\n",
    "t3=torch.Tensor(np.arange(24).reshape((2,3,4)))\n",
    "# 只打印了第一列\n",
    "print(t3[0,:,:])\n",
    "print(t3.dtype)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2., 3., 3., 5.], dtype=torch.float64)\n",
      "tensor([2, 4], dtype=torch.int32)\n",
      "tensor([[1., 1.],\n",
      "        [1., 1.],\n",
      "        [1., 1.]])\n",
      "torch.int32\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 为double类型\n",
    "t5=torch.DoubleTensor([2,3,3,5])\n",
    "print(t5)\n",
    "# 整形\n",
    "a=torch.IntTensor((2,4))\n",
    "print(a)\n",
    "b=torch.ones([3,2],dtype=torch.float32)\n",
    "print(b)\n",
    "# 强转，把 b的浮点型，转换成c的int类型\n",
    "c=b.int()\n",
    "print(c.dtype)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 0., 0.],\n",
      "        [0., 0., 0.]], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "device=torch.device(\"cuda\")\n",
    "a=torch.zeros([2,3],device=device)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0., 0., 0.],\n        [0., 0., 0.]], device='cuda:0')"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这种写法也可以转到gpu\n",
    "b=torch.zeros([2,3])\n",
    "b.to(device)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1.],\n",
      "        [1., 1.]], requires_grad=True)\n",
      "tensor([[3., 3.],\n",
      "        [3., 3.]], grad_fn=<AddBackward0>)\n",
      "tensor([[1., 1.],\n",
      "        [1., 1.]], requires_grad=True)\n",
      "tensor(27., grad_fn=<MeanBackward0>)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "x = torch.ones(2, 2, requires_grad=True)  #初始化参数x并设置requires_grad=True用来追踪其计算历史\n",
    "print(x)\n",
    "#tensor([[1., 1.],\n",
    "#        [1., 1.]], requires_grad=True)\n",
    "\n",
    "y = x+2\n",
    "print(y)\n",
    "#tensor([[3., 3.],\n",
    "#        [3., 3.]], grad_fn=<AddBackward0>)\n",
    "\n",
    "z = y*y*3  #平方x3\n",
    "print(x)\n",
    "#tensor([[27., 27.],\n",
    "#        [27., 27.]], grad_fn=<MulBackward0>)\n",
    "\n",
    "out = z.mean() #求均值\n",
    "print(out)\n",
    "#tensor(27., grad_fn=<MeanBackward0>)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import torch\n",
    "a = torch.randn(2, 2)\n",
    "a = ((a * 3) / (a - 1))\n",
    "print(a.requires_grad)  #False\n",
    "a.requires_grad_(True)  #就地修改\n",
    "print(a.requires_grad)  #True\n",
    "b = (a * a).sum()\n",
    "print(b.grad_fn) # <SumBackward0 object at 0x4e2b14345d21>\n",
    "with torch.no_grad():\n",
    "    c = (a * a).sum()  #tensor(151.6830),此时c没有gard_fn\n",
    "\n",
    "print(c.requires_grad) #False"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.]], requires_grad=True)\n",
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.]])\n"
     ]
    }
   ],
   "source": [
    "a=torch.ones([2,3],requires_grad=True)\n",
    "print(a)\n",
    "print(a.data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}